package com.ccjq.shosestore.web.controller.member;

import com.ccjq.shosestore.member.bo.User;
import com.ccjq.shosestore.member.service.UserService;
import com.ccjq.shosestore.member.service.impl.UserServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private static final String FALG_REG = "reg";//注册页面
    private static final String FALG_REG_SUBMIT = "rs";//提交注册
    private static final String FALG_LOGIN = "login";//登录页面
    private static final String FALR_LOGIN_SUBMIT = "ls";//提交登录
    private static final String FALR_INFO = "info";//用户中心页面
    private static final String FALR_LOGOUT = "logout";//注销

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag = request.getParameter("flag");
        System.out.println("flag = " + flag);

        if (StringUtils.isBlank(flag)) {//跳转到登录页面
            toLoginPage(request,response);
        } else if (FALG_REG.equals(flag)) {//跳转到注册页面
            toRegisterPage(request,response);
        } else if (FALG_REG_SUBMIT.equals(flag)) {//执行注册动作
            register(request, response);
        } else if (FALR_INFO.equals(flag)) {//跳转到用户中心
            toUserInfo(request,response);
        } else if (FALR_LOGOUT.equals(flag)) {//用户注销
            logout(request,response);
        } else if (FALG_LOGIN.equals(flag)) {//跳转到登录页面
            toLoginPage(request,response);
        } else if (FALR_LOGIN_SUBMIT.equals(flag)) {//执行登录
            login(request,response);
        }
    }

    private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String mobilePhone = request.getParameter("phone");
        String password = request.getParameter("password");
        String vicode = request.getParameter("vicode");
        HttpSession session = request.getSession();
        String checkcode = (String) session.getAttribute("checkcode");//验证码
        if (checkcode.equalsIgnoreCase(vicode)) {//不区分大小写
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setMobile_phone(mobilePhone);
            String userId = userService.saveUser(user);
            User user2 = userService.findUserById(userId);
            session.setAttribute("user", user2);
            request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request, response);
            return; //终止方法执行
        } else {
            request.setAttribute("message", "验证码不正确");
            request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request, response);
        }
    }
    /**
     * 跳转到注册页面
     *
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     * */
    private void toRegisterPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/register.jsp").forward(request,response);
    }

    private void toUserInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        String userId = request.getParameter("userId");
        User user = userService.findUserById(userId);
        session.setAttribute("user", user);
        request.getRequestDispatcher("/WEB-INF/jsp/member/userinfo.jsp").forward(request, response);
    }

    private void toLoginPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsp/member/login.jsp").forward(request,response);
    }

    /**
     *
     * 提交登录信息 并校验
     * */
    private void login(HttpServletRequest req,HttpServletResponse response) throws ServletException,IOException{
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("vicode");

        System.out.println("用户输入："+code);
        HttpSession session = req.getSession();
        String checkcode = (String) session.getAttribute("checkcode");//验证码
        System.out.println("session中存储的验证码："+checkcode);
        if(code.equalsIgnoreCase(checkcode)){
            User user = userService.findUserByNamePw(username, password);
            if(user != null){//登录成功
                System.out.println("登录成功");
                req.getSession().setAttribute("username", username);
                req.getSession().setAttribute("user", user);
                req.getRequestDispatcher("/pro?flag=index").forward(req,response);
            }else{//登录失败
                //不能用session共享 这样在一个浏览器中请求一个链接，结果就会是共享的
                req.setAttribute("message", "用户不存在");
                req.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(req,response);
            }
        }else{
            System.out.println("失败");
            req.setAttribute("message", "验证码不正确");
            req.getRequestDispatcher("WEB-INF/jsp/member/login.jsp").forward(req, response);
        }

    }
    /**
     * 注销,其实就是将sessio中保存的user信息清除
     * @param req
     * @param response
     * @throws IOException
     * @throws ServletException
     * */
    private void logout(HttpServletRequest req,HttpServletResponse response) throws ServletException,IOException{
        HttpSession session = req.getSession();
        User user = (User)session.getAttribute("user");
        if(user != null){//将session中的用户信息删掉 session立即失效
            session.setAttribute("user", null);
            //session.invalidate();
        }
        req.getRequestDispatcher("pro?flag=index").forward(req, response);
    }
}
